/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
 var addTwoNumbers = function(l1, l2) {
  function ListNode (val, next) {
      this.val = (val===undefined ? 0 : val)
      this.next = (next===undefined ? null : next)
  }
  const getCurr = (v) => v ? v.val : 0
  const getNext = (v) => v ? v.next : v
  let now = 0
  let add = 0
  let result = null
  let temp = null
 
  while (l1 || l2) {
      now = getCurr(l1) + getCurr(l2) + add
      add = (now >= 10) * 1
      now = now >= 10 ? now - 10 : now
      if (!result) {
          result = new ListNode(now)
          temp = result
      } else {
          temp.next = new ListNode(now);
          temp = temp.next;
      }
      l1 = getNext(l1);
      l2 = getNext(l2);
  }
  if (add) temp.next = new ListNode(add);
  return result
 
};